<template>
  <el-dialog :visible.sync="show" title="新增员工" @close="qx">
    <div>
      <el-form ref="form" :rules="rules" label-width="120px" :model="form">

        <el-form-item label="姓名" placeholder="请输入姓名" prop="username">
          <el-input v-model="form.username" class="a" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile" placeholder="请输入手机">

          <el-input v-model="form.mobile" class="a" />
        </el-form-item>

        <el-form-item label="入职时间" prop="timeOfEntry" placeholder="请输入入职时间">
          {{ form }}
          <el-date-picker v-model="form.timeOfEntry" prop="" value-format="yyyy-MM-dd" /> placeholder="选择日期" class="a" />
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment" placeholder="请输入聘用形式">
          <el-select v-model="form.formOfEmployment" class="a">
            <el-option label="你那" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber" placeholder="请输入工号">
          <el-input v-model="form.workNumber" class="a" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName" placeholder="请输入部门">
          <el-input v-model="form.departmentName" class="a" @focus="focus" />
          <el-tree v-if="iffales" :data="list" :props="{ label: 'name' }" @node-click="nodeclick" />
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime" placeholder="请输入转正时间">
          <el-input v-model="form.correctionTime" class="a" />
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <div style="text-align: center;">
        <el-button type="primary" @click="qx">取消</el-button>
        <el-button type="primary" @click="qr">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<!--
  部门树形结构步骤
      1.导入api
      2.调用api
      3.存储数据
      4.点击数据显示
-->

<script>
import { companydepartment } from '@/api/zuzhujiagou'
import { sysuserget } from '@/api/yuangon'
export default {
  data() {
    return {
      iffales: false,
      show: false,
      value: '',
      list: [],
      form: {
        username: '', // 姓名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { trigger: 'change', required: true, message: '请输入用户名' }
        ],
        mobile: [
          { trigger: 'change', required: true, message: '请输入用户名' },
          {
            pattern: /^1[3,6]\d{9}$/, trigger: 'change', message: '请输入正确号码'
          }
        ],
        formOfEmployment: [
          { trigger: 'change', required: true, message: '请输入用户名' }
        ],
        workNumber: [
          { trigger: 'change', required: true, message: '请输入用户名' },
          { pattern: /\d{3,9}/, trigger: 'change', message: '请输入数字' }

        ],
        departmentName: [
          { trigger: 'ch', required: true, message: '请输入用户名' }
        ],
        timeOfEntry: [
          { trigger: 'change', required: true, message: '请输入用户名' }
        ],
        correctionTime: [
          { trigger: 'change', required: true, message: '请输入用户名' }
        ]
      }
    }
  },
  created() {

  },
  methods: {
    async bmlist() {
      // 性能优化判断 数据是否为空在请求
      if (this.list.length === 0) {
        const res = await companydepartment()
        this.list = this.xxxx(res.data.data.depts)
        console.log(this.list)
      }
    },
    // 树形转换数据
    xxxx(arr, pud = '') {
      return arr.filter((v) => {
        if (v.pid === pud) {
          v.children = this.xxxx(arr, v.id)
          return true
        }
      })
    },
    nodeclick(a) {
      console.log(a)
      this.form.departmentName = a.name
      this.iffales = false
    },
    // 部门聚焦
    focus() {
      // 性能优化，当点击部门聚焦在发送请求
      this.bmlist()

      this.iffales = true
    },
    // 确认
    async qr() {
      await this.$refs.form.validate()
      await sysuserget(this.form)

      this.show = false
      this.$emit('getdata')
      this.$message.success('成功添加')
    },
    // 取消
    qx() {
      this.show = false
      this.$refs.form.resetFields()
      this.form = {
        username: '', // 姓名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        correctionTime: '' // 转正时间
      }
    }
  }
}
</script>

<style scoped lang="scss">
.a {
  width: 200px;
}
</style>
